﻿using System;
using System.Data;
using System.Data.Common;
using Com.Iooogle.Bms.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Com.Iooogle.Bms.Business
{
	/// <summary>
	/// 用户处理业务类。
	/// </summary>
	public class TProjects : IBmsBusiness
	{
		#region 扩展的成员方法。

		/// <summary>
		/// 检查项目名称是否存在。
		/// </summary>
		/// <param name="sProjectName">指定一个项目名称。</param>
		/// <returns></returns>
		public bool Exists(string sProjectName)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT COUNT(*) FROM `bms_projects` WHERE `ProName` = @ProName");
			db.AddInParameter(cmd, "ProName", DbType.String, sProjectName);
			Int32 obj = Convert.ToInt32(db.ExecuteScalar(cmd));

			return (obj == 0) ? false : true;
		}

		#endregion

		#region 标准 IBmsBusiness 成员。

		/// <summary>
		/// 添加记录。
		/// </summary>
		/// <param name="entity">指定数据实体对象。</param>
		/// <returns>返回最后插入的主键值。</returns>
		public object Create(IDataEntity entity)
		{
			ProjectsTable tbl = ( ProjectsTable ) entity;

			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("INSERT INTO `bms_projects`(`ProName`, `BugCount`) VALUES(@ProName, 0);SELECT LAST_INSERT_ID();");
			db.AddInParameter(cmd, "ProName", DbType.String, tbl.ProName);
			object obj = db.ExecuteScalar(cmd);

			return obj;
		}

		/// <summary>
		/// 更新记录。
		/// </summary>
		/// <param name="entity">指定数据实体对象。</param>
		/// <returns>返回受影响的记录条数。</returns>
		public int Update(IDataEntity entity)
		{
			ProjectsTable tbl = ( ProjectsTable ) entity;

			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("UPDATE `bms_projects` SET `ProName` = @ProName, `BugCount` = @BugCount WHERE `ProID` = @ProID");
			db.AddInParameter(cmd, "ProName", DbType.String, tbl.ProName);
			db.AddInParameter(cmd, "BugCount", DbType.Int32, tbl.BugCount);
			db.AddInParameter(cmd, "ProID", DbType.Int32, tbl.ProID);
			int obj = db.ExecuteNonQuery(cmd);

			return obj;
		}

		/// <summary>
		/// 删除记录。
		/// </summary>
		/// <param name="key">指定主键标识值。</param>
		public void Delete(object key)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("DELETE FROM `bms_projects` WHERE `ProID` = @ProID");
			db.AddInParameter(cmd, "ProID", DbType.Int32, key);
			db.ExecuteNonQuery(cmd);
		}

		/// <summary>
		/// 获取全部记录。
		/// </summary>
		/// <returns></returns>
		public DataSet GetAll()
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT `ProID`, `ProName`, `BugCount` FROM `bms_projects`");
			DataSet ds = db.ExecuteDataSet(cmd);

			return ds;
		}

		/// <summary>
		/// 获取数据总数。
		/// </summary>
		/// <returns></returns>
		public int GetCount()
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT COUNT(*) FROM `bms_projects`");
			Int32 obj = Convert.ToInt32(db.ExecuteScalar(cmd));

			return obj;
		}

		/// <summary>
		/// 获取单行记录。
		/// </summary>
		/// <param name="key">指定主键标识值。</param>
		/// <returns></returns>
		public IDataEntity GetRow(object key)
		{
			ProjectsTable entity = null;
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = db.GetSqlStringCommand("SELECT `ProID`, `ProName`, `BugCount` FROM `bms_projects` WHERE `ProID` = @ProID");
			db.AddInParameter(cmd, "ProID", DbType.Int32, key);
			using (IDataReader dr = db.ExecuteReader(cmd)) {
				if (dr.Read()) {
					entity = new ProjectsTable();
					entity.ProID = Convert.ToInt32(dr["ProID"]);
					entity.ProName = Convert.ToString(dr["ProName"]);
					entity.BugCount = Convert.ToInt32(dr["BugCount"]);

					dr.Close();
				}
			}

			return entity;
		}

		#endregion
	}
}